﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Azad;
using NazSoft.Framework;
using NazSoft.Framework.Extensions;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var collection = Request.QueryString;
        String mn = collection["mn"] ?? "E-Paper"; // menu name
        string mp = collection["mp"];
        bool preview = Convert.ToBoolean(collection["preview"]);
        this.Page.Title = "Daily Extra News | ";
        if (Page.IsPostBack == false)
        {
            SilverlightService silverlightService = new SilverlightService();
            var today = DatetimeUtility.GetDateFromQueryString(collection);

            var logoInfo = silverlightService.GetByCurrentId(today, preview);

            if (logoInfo != null)
            {
                // over-ride date here to be used later in databound method
                today = logoInfo.CurrentDate; ;

                var details = logoInfo.Detail.Where(x => x.MenuId == 11).FirstOrDefault();

                if (details != null)
                {
                    // add tags to page meta tags
                    this.Page.MetaKeywords.Insert(0, details.Tags);
                    //this.Page.AddMetaTags("keywords", details.Tags);

                    // get detail to detail
                    var pageSetting = details.DetailToDetail.Where(f => f.MenuName != null && f.MenuName.ToLower() == mn.ToLower()).FirstOrDefault();

                    var detailToDetail = details.DetailToDetail;

                    int index = detailToDetail.IndexOf(pageSetting);

                    var pageSize = details.NoOfFilePerPage;
                    pageSize = 1;//pageSize == 1;//0 ? 4 : pageSize;
                    int currentPage = 0;
                    if (collection["page"] != null)
                        currentPage = collection["page"].ToLower().Replace("page-", "").ToInt32Safe();
                    //var  = (.ToInt32Safe());
                    currentPage = currentPage == 0 ? 1 : currentPage;
                    if (index != -1)
                        currentPage = index + 1;

                    var recordsToSkip = (currentPage - 1) * pageSize;
                    var totalPages = Math.Ceiling((double)(detailToDetail.Count / (double)pageSize));

                    var dataToDisplay = detailToDetail.Skip(recordsToSkip).Take(pageSize);

                    if (currentPage == 1)
                    {
                        if (detailToDetail.Count > pageSize)
                        {
                            NextPageHyperlink.Visible = true;
                            PrevPageHyperLink.Visible = false;
                        }
                        else
                        {
                            NextPageHyperlink.Visible = false;
                            PrevPageHyperLink.Visible = false;
                        }
                    }
                    else if (currentPage == totalPages)
                    {
                        NextPageHyperlink.Visible = false;
                        PrevPageHyperLink.Visible = true;
                    }
                    else
                    {
                        NextPageHyperlink.Visible = true;
                        PrevPageHyperLink.Visible = true;
                    }

                    if (today.HasValue)
                    {
                        var navigateUrl = string.Format("/{0}/{1}/{2}/", today.Value.Year, today.Value.Month.ZeroPad(), today.Value.Day.ZeroPad());


                        NextPageHyperlink.NavigateUrl = navigateUrl + "page-" + (currentPage + 1) + "";
                        PrevPageHyperLink.NavigateUrl = navigateUrl + "page-" + (currentPage - 1) + "";


                        if (mp == "b")
                        {
                            NextPageHyperlink.NavigateUrl = NextPageHyperlink.NavigateUrl + "?mp=b";
                            PrevPageHyperLink.NavigateUrl = PrevPageHyperLink.NavigateUrl + "?mp=b";
                        }

                        if (dataToDisplay != null)
                        {
                            string strMenuName = string.IsNullOrEmpty(dataToDisplay.FirstOrDefault().MenuName) ? "" : " | " + dataToDisplay.FirstOrDefault().MenuName;
                            this.Page.Title = "Daily Extra News | " + today.Value.ToString("D") + " | " + " E-Paper " + strMenuName;//details.MenuName.Replace("-", " ").Capitalize();
                        }
                        var navigateUrl1 = string.Format("/{0}/{1}/{2}/", today.Value.Year, today.Value.Month.ZeroPad(), today.Value.Day.ZeroPad());
                        hdNavigateUrl.Value = navigateUrl1;//navigateUrl + "?mp=b";
                    }

                    NewsRepeater.DataSource = dataToDisplay;
                    NewsRepeater.DataBind();

                    if (details.MenuId == 11)
                    {
                        AajKaAkhbarSharingLinksPlaceHolder.Visible = true;
                        HomePageSharingLinksPlaceHolder.Visible = false;
                    }
                }
                else
                {
                    NextPageHyperlink.Visible = false;
                    PrevPageHyperLink.Visible = false;

                    this.Page.Title = "Daily Extra News | " + today.Value.ToString("D") + " | " + mn.Replace("-", " ").Capitalize();


                    // set default meta tags
                    //this.Page.AddMetaTags("keywords", string.Empty);
                }


                SetMagnify();


            }
            else
            {
                NewsRepeater.Visible = false;
                NoNewsMessage.Visible = true;
                NextPageHyperlink.Visible = false;
                PrevPageHyperLink.Visible = false;


                // set default meta tags
                //this.Page.AddMetaTags("keywords", string.Empty);
            }
        }
    }
    public string NavigateUrl { get; set; }
    private void SetMagnify()
    {

        if (Request.QueryString["mag"] == "true")
        {
            string query = @"jQuery(document).ready(function ($) { 
                    $('.magnifiable').each(function(){

                            $(this).addpowerzoom({defaultpower: 1.4,
                        

                            powerrange: [80, 80],largeimage: true,magnifiersize: [500, 200] }); 
                        });

});";
            //query = string.Format(query, imgFarman.ClientID);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Farman", query, true);

        }
        else
        {

            if (Request.QueryString["mag"] == "false")
            {
                Session["Magni"] = null;
            }





            if (!string.IsNullOrEmpty(Convert.ToString(Session["Magni"])))
            {
                string query = @"jQuery(document).ready(function ($) { 
                    $('.magnifiable').each(function(){

                            $(this).addpowerzoom({defaultpower: 1.4,
                        

                            powerrange: [80, 80],largeimage: true,magnifiersize: [500, 200] }); 
                        });

});";
                //query = string.Format(query, imgFarman.ClientID);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "LogoDetailDetail", query, true);


            }
        }
    }
    protected void NewsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        HtmlAnchor achor = e.Item.FindControl("newsAnchorImage") as HtmlAnchor;
        if (achor != null)
        {
            achor.HRef = hdNavigateUrl.Value;
        }

    }
}